//
//  LoginViewController.m
//  huanxin
//
//  Created by 郭亚 on 16/4/28.
//  Copyright © 2016年 郭亚. All rights reserved.
//

#import "LoginViewController.h"

#import "RegisterViewController.h"

#import "MessageViewController.h"

#import "AppDelegate.h"

#import "FriendViewController.h"

#import <EaseMob.h>

@interface LoginViewController ()

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self createView];
    
}

- (void)createView
{
    _userName = [[UITextField alloc] initWithFrame:CGRectMake(10, 60, self.view.frame.size.width - 20, 40)];
    _userName.layer.borderWidth = 0.5;
    _userName.layer.borderColor = [UIColor orangeColor].CGColor;
    _userName.textAlignment = NSTextAlignmentCenter;
    _userName.placeholder = @"请输入用户名";
    [self.view addSubview: _userName];
    
    _passWord = [[UITextField alloc] initWithFrame:CGRectMake(_userName.frame.origin.x, CGRectGetMaxY(_userName.frame) + 10, _userName.frame.size.width, _userName.frame.size.height)];
    _passWord.layer.borderWidth = 0.5;
    _passWord.layer.borderColor = [UIColor orangeColor].CGColor;
    _passWord.textAlignment = NSTextAlignmentCenter;
    _passWord.placeholder = @"请输入密码";
    [self.view addSubview: _passWord];
    
    
    _loginButton = [[UIButton alloc] initWithFrame:CGRectMake(_passWord.frame.origin.x, CGRectGetMaxY(_passWord.frame) + 10, _passWord.frame.size.width, _passWord.frame.size.height)];
    _loginButton.backgroundColor = [UIColor orangeColor];
    _loginButton.layer.cornerRadius = 5;
    [_loginButton setTitle:@"登录" forState:(UIControlStateNormal)];
    [_loginButton addTarget:self action:@selector(loginButtonClicked) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:_loginButton];
    
    _registerButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 150, CGRectGetMaxY(_loginButton.frame) + 10, 140, _loginButton.frame.size.height)];
    [_registerButton setTitle:@"没账号? 去注册" forState:(UIControlStateNormal)];
    [_registerButton setTitleColor:[UIColor orangeColor] forState:(UIControlStateNormal)];
    [_registerButton addTarget:self action:@selector(registerButtonClicked) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:_registerButton];
    
}

- (void)loginButtonClicked
{
    
      [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:self.userName.text password:self.passWord.text completion:^(NSDictionary *loginInfo, EMError *error) {
          if (error == nil) {
              AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
              UINavigationController *messageVC = [[UINavigationController alloc] initWithRootViewController:[[MessageViewController alloc] init]];
              messageVC.title = @"消息";
              UINavigationController *FriendVC = [[UINavigationController alloc] initWithRootViewController:[[FriendViewController alloc] init]];
              FriendVC.title = @"好友";
              
              UITabBarController *tabBar = [[UITabBarController alloc] init];
              tabBar.viewControllers = @[messageVC, FriendVC];
              app.window.rootViewController  =tabBar;

          } else {
              NSLog(@"登录失败");
          }
      } onQueue:dispatch_get_main_queue()];
}

- (void)registerButtonClicked
{
    RegisterViewController *registerVC = [[RegisterViewController alloc] init];
    __weak LoginViewController *pSelf = self;
    registerVC.block = ^(NSString *userName, NSString *passWord) {
        pSelf.userName.text = userName;
        pSelf.passWord.text = passWord;
    };
    
    [self presentViewController:registerVC animated:YES completion:^{
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
